// components/MaxBureau/MaxBureau.js
import { picUrl, formatTime, dataHandleNull } from '../../utils/util.js';
import { conventionDetail, activityList } from '../../utils/getData.js'
const getopen = require('../../utils/maxutil.js');

const app = getApp();

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    type: { //来源：1是活动详情页，2个人主页, 3局汇总
      type: String,
      value: 0,
      observer: function(newVal, oldVal) {
        this.setData({
          source:newVal
        })

        this.setData({
          headerTop:getApp().globalData.headerTop
        })
      }
    },
    conventionData: { //
      type: Object,
      value: 0,
      observer: function(newVal, oldVal) {
        if(newVal != null){
          let headerdata = {
            conventionId:newVal.conventionId,
            // sponsorId:convention.sponsorId,
            // avatarPath:picUrl('',convention.sponsorUser.avatarPath),
            // userName:convention.sponsorUser.userName,
            islike:newVal.guanzhu,
            shareImg:newVal.shareImg
          }
          this.setData({
            data:newVal,
            headerdata
          })
          console.log(333333)
          console.log(newVal)
          console.log(newVal.headerdata)

        }
        // this.getdetail()
      }
    },

  },

  /**
   * 组件的初始数据
   */
  data: {
    minModel:app.globalData.minModel, //小机型iphone8

    headerStatus:6, //1:箭头分享头像,2箭头头像,3头像关闭图标
    source:1, //来源：1是活动详情页，2个人主页, 3局汇总
    data:{}, //活动数据
    headerdata:{}, //传给头部headers组件的数据
    conventionId:'', //活动ID
    sharePopover:false, //分享层的状态
    headerTop:0,
    currentTab:0, //
    list:[],//活动的局数据
    pageNum: 1, //当前页数
    buttnName:"预订购票",
    goAppPopover: false, //进入有局app弹框状态
    nolist: false //列表数据没有更多
  },

  ready: function (){
    
  },

  /**
   * 组件的方法列表
   */
  methods: {


    //  tab切换逻辑
    swichNav: function( e ) {
      var that = this;

      if( this.data.currentTab === e.target.dataset.current ) {
        return false;
      } else {
          that.setData( {
            currentTab: e.target.dataset.current,
            buttnName:e.target.dataset.current==0?"预订购票":"组局"
          })
      }

      if(that.data.currentTab==1){



        that.setData( {
          pageNum: 1,
          list:[]
        })

        that.getActivityList();
      }

    },
    //tab主体部份滑动事件
    bindChange: function( e ) {
      var that = this;
      that.setData( { currentTab: e.detail.current ,buttnName:e.target.dataset.current===0?"预订购票":"组局"});
    },

    //查询局列表
  getActivityList() {
    let than = this;
    const {
      list
    } = this.data;
    let data = {
      userId: wx.getStorageSync('userId'),
      pageNum: than.data.pageNum,
      pageSize: 10,
      conventionId: than.properties.conventionData.conventionId,
      searchFlag: 0
    }
    activityList(data).then(res => {
      if (res.success) {
        if (res.result.records.length == 0) {
          than.setData({
            nolist: true
          })
        } else {
          let currentTime = formatTime(new Date()); //当前时间
          currentTime = currentTime.substring(0, currentTime.length - 9);
  
          let newDatd = res.result.records.map((item, index) => {
             item = dataHandleNull(item); 
           
            let activityEndTime = item.activityDate.slice(0,10).replace(/-/g, "/"); //局结束时间
            console.log(item.activityMoney)
            console.log(currentTime > activityEndTime)
            let payWay = "所有人AA";
            if(item.activityPayWay==2){
              payWay = "男生AA";
            }else if(item.activityPayWay==3){
              payWay = "女生AA";
            }else if(item.activityPayWay==4){
              payWay = "我请客";
            }
            let tt = "￥"+item.activityAmount+"/人"+"\xa0\xa0\xa0"+payWay;
            
            
            return {
              activityInvalidation: currentTime > activityEndTime, //局是否失效
              activityBlurCoverPage: picUrl('activity/img/', item.activityBlurCoverPage),
              activityCoverPage: picUrl('activity/img/', item.activityCoverPage),
              activityAddress: item.activityAddress.split(',')[0],
              activityBeginTime: item.activityBeginTime,
              activityDate: item.activityDate.slice(5).replace(/-/g, "/"),
              activityEndTime: item.activityEndTime,
              activityId: item.activityId,
              activityImageRGB: item.activityRGB, //
              activityJoinType: item.activityJoinType,
              activityMaxNums: item.activityMaxNums,
              activityName: item.activityName,
              activityRGB: item.activityRGB,
              activityType: item.activityType, //是否为大型活动
              islock: false, //是否为第一次跳转大型活动， false--未锁定  true--锁定
              activityMoney:tt, //
              guanzhu: item.guanzhu, //
              guanzhuTips: item.guanzhuTips, //
              userTips: item.userTips, //
              userTagNames:item.userTagNames,//标签
              userTagName:"哈哈",
              join: item.activityType ==1 ? false :item.join, //
              shareImg: picUrl('activity/img/', item.shareImg), //
              activityUsedNums: item.activityUsedNums,
              activityUserMap:item.activityUserMap.participantlist,
              activityTips:item.activityTips.split(","),
  
              sponsorId: item.sponsorId,
              sponsorUser: {
                ...dataHandleNull(item.sponsorUser) ,
                avatarPath: picUrl('', item.sponsorUser.avatarPath),
                authImgPath: picUrl('activity/img/', item.sponsorUser.authImgPath),
                userPic: picUrl('activity/img/', item.sponsorUser.userPic)
              },
  
            }
          })
          let newDatdtwo = newDatd.map((b) => {7
            return {
              ...b,
              activityUserMap: b.activityUserMap && b.activityUserMap.map((c) => {
                return {
                  ...dataHandleNull(c),
                  authImgPath: picUrl('', c.authImgPath),
                  avatarPath: picUrl('', c.avatarPath),
                  userPic: picUrl('', c.userPic)
                }
              })
            }
          })
          
          than.setData({
            list: list.concat(newDatdtwo)
          })

          console.log("====czq"+than.data.list)
        }

      }

    })
  },
    //滚动到底部 加载更多数据
  lower(e) {

    this.setData({
      goAppPopover: false
    })
    if(this.data.currentTab ===0){
      return false;
    }

    const {
      pageNum,
      nolist
    } = this.data
    if (nolist) return false;
    this.setData({
      pageNum: pageNum + 1
    }, () => {
      this.getActivityList()
    })
  },

    //三个小图标点击事件
    goList(){
      // 大型活动长震动
      // wx.vibrateLong();
      wx.vibrateShort();
      

      // if(wx.getStorageSync('status') == 0){
      //   wx.navigateTo({
      //     url: '../login/login'
      //   })
      //   return false;
      // }

      // this.showmessage();
      wx.navigateTo({
        url: '../bureauList/bureauList?conventionId='+this.data.conventionId
      })
      
    },
     //透明背景事件
     ontransparentBj() {
      this.setData({
        joinpopover: false,
        ischong: true,
        isShare: false,
        goAppPopover: false
      })
    },
     //  打开有局app弹框
     openApp() {
      console.log('打开有局app')

      //是否安卓机
      if(!app.globalData.isiPhone){
        wx.navigateTo({
          url: '../androidMessage/androidMessage'
        })
        return false
      }

      this.setData({
        goAppPopover: true
      })

    },
    //模板消息授权
    showmessage(e){
    console.log(e)
    wx.requestSubscribeMessage({
      tmplIds: ['532UuP9jK6G7QnHaXrOeOfePXlrHXVwQTrQWfTljpo4','Hl7_W9R8X5qaAhXIcSNJYx6ojcBLrjGtlNCQGfpkMTQ'],
      success (res) { 
        console.log(res)
        if (res['532UuP9jK6G7QnHaXrOeOfePXlrHXVwQTrQWfTljpo4','Hl7_W9R8X5qaAhXIcSNJYx6ojcBLrjGtlNCQGfpkMTQ'] == 'accept') { //accept--用户同意 reject--用户拒绝 ban--微信后台封禁,可不管
          console.log('用户同意')

          wx.navigateTo({
            url: '../bureauList/bureauList'
          })
     
      
        }
      },
      fail(res){
        console.log(res)
      }
    })
},

    //导航上关闭事件回调
    _close(){
      console.log('关闭回调啦')
      //触发取消回调
      this.triggerEvent("cancelEvent")
    },


    // //关闭分享弹出层
    closeShare(){
      this.setData({
        sharePopover:false
      })
    },


  },
})
